﻿using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using Microsoft.JCI.Common.Utility;
using System.Collections.Generic;
using Microsoft.JCI.Bll;
using System.Data;
using System;
using Microsoft.JCI.Common.Entity;
using Telerik.WebControls;
using Microsoft.JCI.Dal;
namespace Microsoft.JCI.Portal.BaseManage
{
    public partial class CustomerList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private List<SqlDbParameter> GetQueryCondition()
        {
            List<SqlDbParameter> listParameters = new List<SqlDbParameter>();
            SqlDbParameter Parameter = null;

            if (!string.IsNullOrEmpty(this.txtCusterName.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Name";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Name";
                Parameter.ParameterValue = this.txtCusterName.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.ddCustomerType.SelectedValue))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Type";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Type";
                Parameter.ParameterValue = this.ddCustomerType.SelectedValue;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtCustomerAddress.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Address";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Address";
                Parameter.ParameterValue = this.txtCustomerAddress.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtMobile.Text))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Mobile";
                Parameter.ColumnType = DbType.DateTime;
                Parameter.ParameterName = "@Mobile";
                Parameter.ParameterValue = txtMobile.Text;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtHomePhone.Text))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "HomePhone";
                Parameter.ColumnType = DbType.DateTime;
                Parameter.ParameterName = "@HomePhone";
                Parameter.ParameterValue = txtHomePhone.Text;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtOfficePhone.Text))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "OfficePhone";
                Parameter.ColumnType = DbType.DateTime;
                Parameter.ParameterName = "@OfficePhone";
                Parameter.ParameterValue = txtOfficePhone.Text;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtEmail.Text))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Email";
                Parameter.ColumnType = DbType.DateTime;
                Parameter.ParameterName = "@Email";
                Parameter.ParameterValue = txtEmail.Text;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            return listParameters;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            MasterDataBll bll = new MasterDataBll();
            List<MadCustomerEntity> dt = bll.GetAll<MadCustomerEntity>(GetQueryCondition());
            this.radGridCustomerList.DataSource = dt;
            radGridCustomerList.DataBind();
        }

        protected void radGridCustomerList_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            if (e.RebindReason != Telerik.WebControls.GridRebindReason.InitialLoad)
            {
                MasterDataBll bll = new MasterDataBll();
                List<MadCustomerEntity> dt = bll.GetAll<MadCustomerEntity>(GetQueryCondition());

                this.radGridCustomerList.DataSource = dt;
            }
        }

        protected void RadGrid1_ItemUpdated(object source,  GridUpdatedEventArgs e)
        {
            //if (e.Exception != null)
            //{
            //    e.KeepInEditMode = true;
            //    e.ExceptionHandled = true;
            //    DisplayMessage(true, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " cannot be updated. Reason: " + e.Exception.Message);
            //}
            //else
            //{
            //    DisplayMessage(false, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " updated");
            //}
        }

        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            //if (e.Exception != null)
            //{
            //    e.ExceptionHandled = true;
            //    e.KeepInInsertMode = true;
            //    DisplayMessage(true, "Employee cannot be inserted. Reason: " + e.Exception.Message);
            //}
            //else
            //{
            //    DisplayMessage(false, "Employee inserted");
            //}
        }

        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
        {
            //if (e.Exception != null)
            //{
            //    e.ExceptionHandled = true;
            //    DisplayMessage(true, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " cannot be deleted. Reason: " + e.Exception.Message);
            //}
            //else
            //{
            //    DisplayMessage(false, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " deleted");
            //}
        }

        private void DisplayMessage(bool isError, string text)
        {
            //Label label = (isError) ? this.Label1 : this.Label2;
            //label.Text = text;
        }

        public void CreateCustomer(GridEditFormInsertItem Item)
        {
            MadCustomerEntity entity = new MadCustomerEntity();
            entity.Address = ((System.Web.UI.WebControls.TextBox)Item.FindControl("CustomerAddress")).Text;
            entity.City = ((System.Web.UI.WebControls.DropDownList)Item.FindControl("ddCity")).SelectedValue;
            entity.Country = ((System.Web.UI.WebControls.TextBox)Item.FindControl("County")).Text;
            entity.Email = ((System.Web.UI.WebControls.TextBox)Item.FindControl("Email")).Text;
            entity.Gender =  ((System.Web.UI.WebControls.RadioButtonList)Item.FindControl("Gender")).SelectedValue == "1" ? true:false ;
            entity.HomePhone = ((System.Web.UI.WebControls.TextBox)Item.FindControl("txtHomePhone")).Text;
            entity.Mobile = ((System.Web.UI.WebControls.TextBox)Item.FindControl("Mobile")).Text;
            entity.Name = ((System.Web.UI.WebControls.TextBox)Item.FindControl("CusterName")).Text;
            entity.OfficePhone = ((System.Web.UI.WebControls.TextBox)Item.FindControl("txtOfficePhone")).Text;
            entity.PostCode = ((System.Web.UI.WebControls.TextBox)Item.FindControl("ZipCode")).Text;
            entity.Province = ((System.Web.UI.WebControls.DropDownList)Item.FindControl("ddProvince")).Text;
            entity.Remark = ((System.Web.UI.WebControls.TextBox)Item.FindControl("Remark")).Text;
            if (((System.Web.UI.WebControls.DropDownList)Item.FindControl("ddCustomerType")).SelectedValue != "")
            {
                entity.Type = Convert.ToInt32(((System.Web.UI.WebControls.DropDownList)Item.FindControl("ddCustomerType")).SelectedValue);
            }
           
            entity.CreatedBy = this.CurrentUserEntity.Account;
            entity.CreatedOn = DateTime.Now;

            MasterDataBll bll = new MasterDataBll();
            bll.Insert(entity);

        }
        
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked
            {
                GridEditCommandColumn editColumn = (GridEditCommandColumn)radGridCustomerList.MasterTableView.GetColumn("EditCommandColumn");
                editColumn.Visible = false;

                 
                                    
            }
            else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted)
            {
                e.Canceled = true;
            }
            else
            {
                if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
                {
                    GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
                    CreateCustomer(item);
                }


                GridEditCommandColumn editColumn = (GridEditCommandColumn)radGridCustomerList.MasterTableView.GetColumn("EditCommandColumn");

                if (!editColumn.Visible)
                    editColumn.Visible = true;
            }
            radGridCustomerList.Rebind();

        }

        void obj_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList obj = sender as DropDownList;
            DropDownList cbbCity = obj.Parent.FindControl("ddCity") as DropDownList;

            if (string.IsNullOrEmpty(obj.SelectedValue))
            {
                return;
            }
            SysDictionaryDal dal = new SysDictionaryDal();
            SysDictionaryEntity entity = dal.GetDictionaryByCode(Enumerator.DictionaryType.Province, obj.SelectedValue);

            cbbCity.Items.Clear();
            DataTable dt = dal.LoadChildNode(entity.DictionaryId);
            cbbCity.DataSource = dt;
            cbbCity.DataTextField = "ValueCn";
            cbbCity.DataValueField = "Code";
            cbbCity.DataBind();
            cbbCity.Items.Insert(0, new ListItem("...", ""));
        }
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode && e.Item is GridEditableItem)
            {
                GridEditFormItem item = (GridEditFormItem)e.Item;
                DropDownList obj = (DropDownList)item.FindControl("ddProvince");
                obj.Items.Clear();
                SysDictionaryDal dal = new SysDictionaryDal();
                DataTable dt = dal.LoadDictionaryByType(Enumerator.DictionaryType.Province);
                obj.DataSource = dt;
                obj.DataTextField = "ValueCn";
                obj.DataValueField = "Code";
                obj.DataBind();
                obj.AutoPostBack = true;
                obj.Items.Insert(0, new ListItem("...", ""));
                obj.SelectedIndexChanged += new EventHandler(obj_SelectedIndexChanged);

            }
        }

    }
}
